package com.yangyang.rpc.admin.interceptor;

import com.yangyang.rpc.admin.annotations.IgnoreLogin;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by chenshunyang on 2016/12/11.
 */
public class UserInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //添加IngoreLogin的都忽略 登陆验证
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            IgnoreLogin ignoreLogin = (IgnoreLogin) handlerMethod.getMethodAnnotation(IgnoreLogin.class);
            if (ignoreLogin == null) {
                Object controller = handlerMethod.getBean();
                ignoreLogin = controller.getClass().getAnnotation(IgnoreLogin.class);
            }
            if (ignoreLogin != null) {
                return true;
            }
        }



        return true;// 只有返回true才会继续向下执行，返回false取消当前请求
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
